package com.firegod.servicebase.entity;

import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import java.util.Set;

@Entity
@DiscriminatorValue("1")
@Getter
@NoArgsConstructor
public class Teacher extends User {

    @OneToMany
    @JoinTable(name = "tb_teacher_course")
    @Setter
    private Set<Course> courseList;

    public Teacher(String userId) {
        this.userId = userId;
    }

    public JSONObject show() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userId", userId);
        jsonObject.put("name", name);
        jsonObject.put("email", email);
        jsonObject.put("mobile", mobile);
        jsonObject.put("courseList", courseList.stream().sorted(
                (o1, o2) -> o2.getNumOfStudent() - o1.getNumOfStudent()
        ).limit(3).map(Course::show));
        return jsonObject;
    }

    public int NumberOfCourse() {
        return this.courseList.size();
    }

    public boolean hasCourse() {
        return this.courseList.size() > 0;
    }
}
